<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-form-item label="字段参数" prop="data">
                <sc-form-table ref="table" v-model="form.data" :addTemplate="addTemplate" placeholder="暂无数据" hideAdd hideDelete height="600">
					<el-table-column v-for="(item,index) in list" :key="index" :label="item.name">
						<template #default="scope">
							<el-input v-model="scope.row[item.prop+(scope.$index+1)]" placeholder="请输入变量名" :readonly="item.prop == 'FNAME'"></el-input>
						</template>
					</el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '设置对应表',
				edit: '设置对应表',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
                code:"",
				data: [
                    {FVALUE1:"",FNAME1:""},
                    {FVALUE2:"",FNAME2:""},
                    {FVALUE3:"",FNAME3:""},
                    {FVALUE4:"",FNAME4:""},
                    {FVALUE5:"",FNAME5:""},
                    {FVALUE6:"",FNAME6:""},
                    {FVALUE7:"",FNAME7:""},
                    {FVALUE8:"",FNAME8:""},
                    {FVALUE9:"",FNAME9:""},
                    {FVALUE10:"",FNAME10:""},
                    {FVALUE11:"",FNAME11:""},
                    {FVALUE12:"",FNAME12:""},
                    {FVALUE13:"",FNAME13:""},
                    {FVALUE14:"",FNAME14:""},
                    {FVALUE15:"",FNAME15:""},
                    {FVALUE16:"",FNAME16:""},
                    {FVALUE17:"",FNAME17:""},
                    {FVALUE18:"",FNAME18:""},
                    {FVALUE19:"",FNAME19:""},
                    {FVALUE20:"",FNAME20:""},
                    {FVALUE21:"",FNAME21:""},
                    {FVALUE22:"",FNAME22:""},
                    {FVALUE23:"",FNAME23:""},
                    {FVALUE24:"",FNAME24:""},
                ],

			},
			//验证规则
			rules: {
			},
			list:[
				{
					prop:'FNAME',
					name:'原始字段值'
				},
                {
					prop:'FVALUE',
					name:'软件内变量名'
				},
			]
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					res = await this.$API.jiankong.correspond.add.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success(res.message)
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.code = data.code
            var res = await this.$API.jiankong.correspond.detail.post({code:data.code});
            let originalData = res.data
            let newArray = [];
            for (let i = 1; i <= 24; i++) {
                let fnameKey = "FNAME" + i;
                let fvalueKey = "FVALUE" + i;
                newArray.push({  [fvalueKey]: originalData[0][fvalueKey], [fnameKey]: originalData[0][fnameKey] });
            }
            console.log('newArray :>> ', newArray);
            this.form.data = newArray;
		},
	}
}
</script>

<style></style>
